//
//  AppDelegate.swift
//  microblog
//
//  Created by Apple on 15/11/23.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        //调用
        setupApp()
        //创建winDow
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        //创建tabbar
       // let tabbarVc = WHMainController()

        //设置根控制器
        //window?.rootViewController = tabbarVc
        //window?.rootViewController?.view.backgroundColor = UIColor.orangeColor()
        
        //window?.rootViewController = WHWelcomeController()
        //外部提供流水布局.但是不太好.流水布局需要设置参数,让调用的人来设置参数.封装的不是特别号
        //在WHNewFeatureController里面实现构造函数
        //window?.rootViewController = WHNewFeatureController()
        
        window?.rootViewController = defaultController()
        
        // 设置keywindow和显示
        // ? 表示?前面的变量/常量有值就执行?后面的代码. ?前面没有值,不执行后面的代码
        window?.makeKeyAndVisible()
        
        // Override point for customization after application launch.
        return true
    }
    //判断进入哪个界面 返回一个 ->UIViewController
    private func defaultController() ->UIViewController {
        // 用户是否登录, 没有加载到账号表示没有登录
        if !WHUserAccount.userLogin {
            return WHMainController()
        }
        
        //已经登录
        return isNewFeature() ? WHNewFeatureController():WHWelcomeController()
    }
    
    //判断是否新版本
    private func isNewFeature() -> Bool {
        //获得当前版本
        let currentVersion = Double(NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String)!
        //获取上一次版本号(从偏好设置)
        let sandboxVersionKey = "sandboxVersionKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(sandboxVersionKey)
        
        //保存当前版本号
        NSUserDefaults.standardUserDefaults().setDouble(currentVersion, forKey: sandboxVersionKey)
        NSUserDefaults.standardUserDefaults().synchronize()
        print("currentVersion: \(currentVersion)")
        print("sandboxVersion: \(sandboxVersion)")
        
        //比较
        return currentVersion != sandboxVersion
        
    }
    
    // MARK: - 其他界面切换控制器,需要通过 AppDelegate 来切换
    // 目标控制器有2个: 欢迎界面和MainViewController
    /**
    
    - parameter isMain: true表示切换到MainViewController, false表示切换到欢迎界面
    */
    private func switchrootViewController(isMain: Bool) {
        window?.rootViewController = isMain ? WHMainController() : WHWelcomeController()
    }
    
    // MARK: - 类方法,切换控制器,方便别人调用
    /**
    类方法,切换控制器,方便别人调用
    
    - parameter isMain: true表示切换到MainViewController, false表示切换到欢迎界面
    */
    class func switchViewController(isMain: Bool){
        (UIApplication.sharedApplication().delegate as? AppDelegate)?.switchrootViewController(isMain)
    }

    //设置导航栏皮肤
    func setupApp(){
        let bar = UINavigationBar.appearance()
        bar.tintColor = UIColor.orangeColor()
    }
    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

